package com.pysoft.web.controller.safe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pysoft.safe.dao.entity.RoleEntity;
import com.pysoft.safe.service.RoleService;

@Controller
@RequestMapping("/safe/role")
public class RoleController {
	@Autowired
	private RoleService  roleService;
	
	@RequestMapping(value="/show.action")
	public String show() {
		return "safe/role";
	}
	
	@RequestMapping(value="/tree.action")
	@ResponseBody
	public ResponseEntity<List<RoleEntity>> tree(){
		
		List<RoleEntity> tree = new ArrayList<RoleEntity>();
		RoleEntity root = new RoleEntity();
		root.setId("root");
		root.setPid("0");
		root.setName("系统角色树");
		root.setRemark("系统角色树");
		root.setUserLimit(-1);
		List<RoleEntity> roles = roleService.query(null);
		tree.add(root);
		tree.addAll(roles);
		return new ResponseEntity<List<RoleEntity>>(tree, HttpStatus.OK);
	}
	
	@RequestMapping(value="/modify.action")
	@ResponseBody
	public Map<String, Object> modify(RoleEntity role) {
		Map<String, Object> map = new HashMap<String, Object>(1);
		roleService.modifyRole(role);
		map.put("success", "1");
		return map;
	}
	
	@RequestMapping(value="/add.action")
	@ResponseBody
	public RoleEntity add(String pid) {
		RoleEntity entity = roleService.addRole(pid);
		return entity;
	}

	@RequestMapping(value="/remove.action")
	@ResponseBody
	public Map<String, Object> remove(@RequestParam("id") String id) {
		Map<String, Object> map = new HashMap<String, Object>(1);
		roleService.removeRole(id);
		map.put("success", "1");
		return map;
	}
	
	@RequestMapping(value="/relating.action")
	@ResponseBody
	public Map<String, Object> relatingResource(@RequestParam("roleId") String roleId,  @RequestParam("resourceIdList") List<String> resourceIdList) {
		Map<String, Object> map = new HashMap<String, Object>(1);
		roleService.relatingResource(roleId, resourceIdList);
		map.put("success", "1");
		return map;
	}
	
	@RequestMapping(value="/cancelRelating.action")
	@ResponseBody
	public Map<String, Object> cancelRelatingResource(@RequestParam("roleId") String roleId) {
		Map<String, Object> map = new HashMap<String, Object>(1);
		roleService.cancelRelatingResource(roleId);
		map.put("success", "1");
		return map;
	}
	
	@RequestMapping(value="/getRelatingResource.action")
	@ResponseBody
	public List<String> getRelatingResources(String roleId){
		return roleService.getRelatingResources(roleId);
	}
}
